package com.gao;

import com.gao.mapper.Blog;
import com.gao.mapper.BlogMapper;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

/**
 * 基于纯Java方式
 * <p>
 * 这种方式我没有试出来如何找到映射器的xml
 *
 * @author GaoXi
 * @date 2022/3/3 20:07
 */
public class PureJavaDemo {


    /**
     * 这种方式是在不知道xml 应该放在哪里？？？
     *
     * @param args
     */
    public static void main(String[] args) {
        //environments配置
        PooledDataSource pooledDataSource = new PooledDataSource();
        pooledDataSource.setDriver("com.mysql.cj.jdbc.Driver");
        pooledDataSource.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&&serverTimezone=GMT");
        pooledDataSource.setUsername("root");
        pooledDataSource.setPassword("888888");
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, pooledDataSource);

        Configuration configuration = new Configuration(environment);
        configuration.addMapper(BlogMapper.class);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        System.out.println(sqlSessionFactory.openSession());
        BlogMapper mapper = sqlSessionFactory.openSession().getMapper(BlogMapper.class);
        Blog blog = mapper.selectBlog(1L);
        System.out.println(blog);
    }

}
